package com.zjc.mymq.broker.core;

import com.zjc.mymq.broker.cache.CommonCache;
import com.zjc.mymq.common.constants.BrokerConstants;
import com.zjc.mymq.common.dto.MessageDTO;

import java.io.IOException;

/**
 * @author zjc
 * @date Created in 22:54 2024/6/25
 */
public class CommitLogAppendHandler {

	public void prepareMMapLoading(String topicName) throws IOException {
		CommitLogMMapFileModel mapFileModel = new CommitLogMMapFileModel();
		mapFileModel.loadFileInMMap(topicName, 0, BrokerConstants.COMMIT_LOG_DEFAULT_MMAP_SIZE);
		CommonCache.getCommitLogMMapFileModelManager().put(topicName, mapFileModel);
	}

	public void appendMsg(MessageDTO messageDTO) throws IOException {
		CommitLogMMapFileModel mapFileModel = CommonCache.getCommitLogMMapFileModelManager().get(messageDTO.getTopic());
		if (mapFileModel == null) {
			throw new RuntimeException("topic is invalid!");
		}
		mapFileModel.writeContent(messageDTO, true);
	}

}
